<?php
//set required scripts
require("HTMLFunctions.php");
require("ValidationFunctions.php");
require('ac_db.inc.php');

//set error reporting
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

//set credentials for database
setcredentials("tr2286","databases8620", "w4111b.cs.columbia.edu/ADB");

//create DB instance
$db = new Db("Proj1", "Part3");


//set email as permanent variable as it will be constantly used
$email;
$errorText = "";

//see what is passed to the script to determine further action
//ensure that input is not blank which would mean that the site is illegally accessed
if(count($_POST) == 0){
	
	//if so, redirect to login
	$db->closeSession();
	echo "<meta http-equiv='refresh' content='0;url=http://localhost/login.php'>";
	exit;
}

//see if only email was passed
if(count($_POST) == 1){
	
	//we know that we were redirected from the profile page so obtain email
	$email = $_POST['email'];
	
	displaySearch($email, $errorText, $db);
}
//otherwise a search query was passed to the script so process that
else{
	
	$email = $_POST['email'];
	$ticker = $_POST['ticker'];
	$name = $db->getStockName($ticker);
	
	if($name == null){
		$errorText = "<h4><font color=\"red\">Invalid Ticker.<br>Please try again.</font></h4>";
		displaySearch($email, $errorText, $db);
	}
	else{
		//show success
		showSuccess($email, $ticker, $db);
		
	}
}

//function to display the search page
function displaySearch($email, $errorText, $db){
	
	//create the html beginning
	HTMLFunctions::printHTMLHead("Search for Stocks");
	
	//create header bar
	HTMLFunctions::generateTitleBar("Search for Stocks", $email, $db);
	
	//now call funciton to display the actual search and content
	generateSearchHTML($email, $errorText, $db);
	
	//close db session
	$db->closeSession();
	
	//call function to create the end
	HTMLFunctions::printHTMLEnd();
}

//function to generate the html content of the search page
function generateSearchHTML($email, $errorText, $db){
	
	//print the form hmtl
	echo "<div class=\"content\">";

	//print any potential error messages passed to the function
	echo "<p>$errorText</p>";
	
	#print actual form
	echo <<<END_HTML
	<p>Search for stocks by entering their ticker symbol</p>
	<table>
	<td align="top">
			<form action="searchStocks.php" method=POST>
			<tr>
				<!-- stock ticker name-->
				<td><p>Ticker or Name:*</p></td>
				<td><input type="text" name="ticker"></td>
				
				<!-- now create a hidden field that resubmits the email so that the script knows what user is being passed back-->
				<input type="hidden" name="email" value="$email">
			</tr>
			<tr>
				<!--submit button-->
				<td><input type="submit" value="Submit"></form></td></tr><tr><td>
END_HTML;

	//create cancel button
	//create array for button
	$parameterArray = array();
	$parameterArray['email'] = $email;
		
	//call subroutine to display button that returns the user to the profile
	HTMLFunctions::generateButton("profile.php", "Cancel", $parameterArray);
		
	#print remaining tags
	echo <<<END_HTML
		</td>
		</tr>
		</table>
END_HTML;
}

//function for redirect page
function showSuccess($email, $ticker, $db){
	
	//call funcitons for html code
	//create the html beginning
	HTMLFunctions::printHTMLHead("Search Reult");
	
	//create header bar
	HTMLFunctions::generateTitleBar("Your Search Result", $email, $db);
	
	$companyName = $db->getStockName($ticker);

	echo <<<END_HTML
	
	<p>You Selected $ticker--- $companyName.</p>
	<p>Click below to see detailed stock information</p>
	
END_HTML;

	//create array for button
	$parameterArray = array();
	$parameterArray['email'] = $email;
	$parameterArray['ticker'] = $ticker;
	
	//call subroutine to display button that returns the user to the profile
	HTMLFunctions::generateButton("stockDetail.php", "Get Detailed Information", $parameterArray);
	
	//close db connection
	$db->closeSession();
	
	//call function to create the end
	HTMLFunctions::printHTMLEnd();
}


